let app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
import util from "../../../utils/util";
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    faceServetitle: "serve-title",
    query: {
      title: "",
      recharge_member_mobile: "", //充值会员手机
      recharge_merchant_mobile: "",
      recharge_total: 1,
      amount: "",
      points: 0
    },
    amount: "",
    setting: {},
    merchant_love_value: 0,
    checked: false,
    flag: false,
    isActive: false,
    shopsActive: false,
    errorText: "",
    ShopsErrorText: "",
    calculate:'',
    loveValue:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // wx.setNavigationBarColor({backgroundColor: })
    this.orderSetting();
  },
  orderSetting() {
    let urlStr = app.getNetAddresss("plugin.face-to-face.frontend.recharge-order.set");
    let json = {};
    app._getNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          console.log(data);
          this.setData({
            setting:data,
          })
          if(data.merchant_love === "fixed") {
            this.setData({
              merchant_love_value: data.merchant_love_value
            })
          } 
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: this.data.themeBaseColor,
          animation: {
            duration: 400,
            timingFunc: 'easeIn'
          }
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  multiplication(data) {
    let urlStr = app.getNetAddresss("plugin.face-to-face.frontend.recharge-order.multiplication");
    let json =  {
      data:data
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          console.log(data);
          this.setData({
            'query.amount': data
          })
          let dataLove = [this.data.setting.merchant_love_value,this.data.amount,this.data.setting.integral_love_ratio?.ratio];
          if(this.data.setting?.merchant_love === "ratio") {
            this.multiplicationLove(dataLove);
          }
          
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
        
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  multiplicationLove(data) {
    let urlStr = app.getNetAddresss("plugin.face-to-face.frontend.recharge-order.multiplication");
    let json =  {
      data:data
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          this.setData({
            merchant_love_value: data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
        
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  monitorMoblie(event) {
    let res = /^[1][3,4,5,6.7,8,9][0-9]{9}$/;
    let value = event.detail;
    if (res.test(value)) {
      this.member(value, "consumer");
      this.setData({
        isActive: false
      })
    } else {
      this.setData({
        isActive: true,
        errorText: '请填写正确的手机号'
      })
    }
  },
  merchantMoblie(event) {
    let res = /^[1][3,4,5,6.7,8,9][0-9]{9}$/;
    let value = event.detail;
    if (res.test(value)) {
      this.member(value, "shops");
      this.setData({
        shopsActive: false
      })
    } else {
      this.setData({
        shopsActive: true,
        ShopsErrorText: '请填写正确的手机号'
      })
    }
  },
  changeprice(event) {
    console.log(event);
    let value = event.detail;
    this.setData({
      amount : value.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3"),
    })
    let data = [this.data.amount,this.data.setting.pay_ratio];
     // 防抖
     if (this.timer) clearTimeout(this.timer);
     this.timer = setTimeout(() => {
       util.debounce(this.multiplication(data));
     }, 500);
    
  },
  member(mobile, name) {
    let urlStr = app.getNetAddresss("plugin.face-to-face.frontend.recharge-order.verify-member");
    let json = {
      mobile
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
            if (name === "consumer") {
              this.setData({
                'query.recharge_member_mobile': data.mobile,
                errorText: ''
              });
            } else {
              this.setData({
                'query.recharge_merchant_mobile': data.mobile,
                ShopsErrorText: ''
              });
            }
        } else {
          if(name === "consumer") {
            this.setData({
              isActive: true,
              errorText: res.msg
            })
          } else {
            console.log(res.msg);
            this.setData({
              shopsActive: true,
              ShopsErrorText: res.msg
            })
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  sellChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  blurinput(event) {
    let value = event.detail;
    this.setData({
      'query.title': value,
    });
  },
  totalOnChange(event) {
    this.setData({
      'query.recharge_total': event.detail
    })
  },
  orderPay() {
    if (!this.data.query.title) {
      wx.showToast({
        icon: 'none',
        title: '请输入商品名称或服务名称',
        duration: 1500
      });
      return;
    }
    if (!this.data.query.recharge_total) {
      wx.showToast({
        icon: 'none',
        title: '请输入商品数量',
        duration: 1500
      });
      return;
    }
    if (!this.data.query.recharge_member_mobile) {
      wx.showToast({
        icon: 'none',
        title: '请输入会员手机号',
        duration: 1500
      });
      return;
    }
    if (!this.data.query.recharge_merchant_mobile) {
      wx.showToast({
        icon: 'none',
        title: '请输入商家会员手机号',
        duration: 1500
      });
      return;
    }
    if (!this.data.query.amount) {
      wx.showToast({
        icon: 'none',
        title: '请输入金额',
        duration: 1500
      });
      return;
    }
    if (this.data.isActive) {
      wx.showToast({
        icon: 'none',
        title: this.data.errorText,
        duration: 1500
      });
      return;
    }
    if (this.data.shopsActive) {
      wx.showToast({
        icon: 'none',
        title: this.data.ShopsErrorText,
        duration: 1500
      });
      return;
    }
    if (!this.data.checked) {
      wx.showToast({
        icon: 'none',
        title: '请勾选协议',
        duration: 1500
      });
      return;
    }
    this.setData({
      flag:true
    })
    let urlStr = app.getNetAddresss("plugin.face-to-face.frontend.recharge-order.create");
    let json = this.data.query;
    let amount = this.data.amount;
    let toFix = Number(amount).toFixed(2) || '0.00';
    this.data.query.amount = this.data.query.amount.toString();
    this.data.query.points = toFix.toString();
    app._getNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          let orderJson = {
            order_ids: data.order_ids,
            api: 'plugin.face-to-face.frontend.recharge-order.index'
          };
          setTimeout(() => {
            wx.navigateTo({
              url: `/packageD/buy/orderPay/orderPay?order_id=${orderJson.order_ids}&api=${orderJson.api}`,
            })
            this.setData({
              flag:false
            })
          },1000);
        } else {
          setTimeout(() => {
            this.setData({
              flag:false
            })
          },1000);
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          wx.hideLoading();
        }
      },
      fail: function (res) {
        setTimeout(() => {
          this.setData({
            flag:false
          })
        },1000);
      }
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})